<?php

/*

***************************************
*                                     *
* Copyright 2011 David Matthew Pugh   *
* Contact Info:thebravedave@gmail.com *
*                                     *
***************************************

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

if(class_exists('stringgenerator') != true)
{
   include('stringgenerator.php');   
}



class WidgetShoppingCart 
{

	public  $items = array();
    public  $identifier;
	// Method that returns a Boolean
	// value indicating if the cart is empty:
	public function is_empty() {
		if (empty($this->items)) {
			return true;
		} else {
			return false;
		}
	}          


	//method for adding items to the cart array, which will be put in as a session variable.  It takes the specific widgets id and the cart array
	public function add_item($id, $info) 
    {
	
		// Is it already in the cart?
		if (isset($this->items[$id])) 
        {
		
            $gw_id = $this->items[$id];
            $qty = $this->items[$id]['qty'];
            
			// Call the update_item() method:
			$this->update_item($id, $this->items[$id]['qty'] + 1);
			
		}
        else 
        {		
			// Add the array of info:
			$this->items[$id] = $info;
			
			// Add the quantity.
			$this->items[$id]['qty'] = 1;
		}
		
	} // end of the add_item method

    //method for adding the random identifier to the cart. this will be used to match up our shipping info/cost/payment info during checkout
    public function add_identifier()
    {
        $stringgenerator = new stringgenerator();
        $random = $stringgenerator->generaterandomstring();
        $this->identifier = $random;
        
    }

	// Method for updating an item in the cart. It takes the specific widgets id and the cart array    
	public function update_item($id, $qty) 
    {
	
		// if quantity is zero, send the request to delete_item method, which will delete it.
		if ($qty == 0) {
		
			$this->delete_item($id);
		
		} elseif ( ($qty > 0) && ($qty != $this->items[$id]['qty'])) {

			// Update the quantity if the quantity value is not zero.
			$this->items[$id]['qty'] = $qty;
		}

	} // End of update_item() method.


	// Method for deleting an item in the cart. Takes the id of the item.
	public function delete_item($id) 
    {
		// Confirm that it's in the cart:
		if (isset($this->items[$id])) 
        {
			// Remove the item:
			unset($this->items[$id]);		
		}		
	} // End of delete_item() method.


	// Method for displaying the cart.
	public function display_cart($action = false) {
	
		// Print the table that will hold the items:
		echo '<table border="0" width="90%" cellspacing="2" cellpadding="2" align="center">
		      <tr>
			    <td align="left" width="20%"><b>Item Name</b></td>
			    <td align="left" width="15%"><b>category</b></td>
			    <td align="left" width="15%"><b>Price/unit</b></td>
			    
			    <td align="center" width="10%"><b>Qty</b></td>
			    <td align="right" width="15%"><b>Total Price</b></td>
		      </tr>
	         ';     
        
	
		// Print form code, if appropriate.
		if ($action) 
        {
			echo '<form action="' . $action . '?page_title=Cart Contents" method="post">
		    <input type="hidden" name="do" value="update" />';
		}

		// Initialize the total:
		$total = 0;

        
		// Loop through each item:   
		foreach ($this->items as $id => $info) 
        {
		
			// Calculate the total and subtotals:
			$subtotal = $info['qty'] * $info['price']; 
			$total += $subtotal;
			$subtotal = number_format($subtotal, 2); 
			 
			// Determine how to show the quantity: 
			$qty = ($action) ? "<input type=\"text\" size=\"3\" name=\"qty[$id]\" value=\"{$info['qty']}\" />" :  $info['qty'];
			$gw_id = $info['gw_id'];
            $sw_id = $info['sw_id'];
            $gw_name = $info['name']; 
            if($sw_id == "")
            {
                $xml_build = $gw_name;
            }
            else
            {
                $gw_name = urldecode($gw_name);  
                
                $xml_desc = new SimpleXMLElement($gw_name);
                $original_name = $xml_desc->gw_name;
                $xml_build = "";
                foreach($xml_desc->Item as $eachitem)
                {
                    $category = $eachitem->category;
                    $category = (string)$category;
                    $descriptor = $eachitem->descriptor;
                    $descriptor= (string)$descriptor;
                    $xml_build .= $category . ':' . $descriptor . ', ';
                }
                //must remove last comma from string before we complete the xml build
                
             
                 $xml_build=substr($xml_build, 0, -2); 
                 
                $xml_build = $original_name . "<br />" . $xml_build;
            }
			// Print the row:
echo <<<EOT

<tr>
	<td align="left">$xml_build</td>
	<td align="left">{$info['category']}</td>
	<td align="left">\${$info['price']}</td>
	<td align="center">$qty</td>
    <td align="right">\$$subtotal</td>
</tr>\n

EOT;

		} // End of FOREACH loop.
		
      
        $plus_shipping = "+ Shipping";
        $_SESSION['total'] = $total;
         
         
		// Complete the table:  
echo '	<tr>
        <td colspan="5" align="right"><b>Total:<b></td>
        <td align="left"><input type="hidden" name="total" value="' . $total . '">' . number_format ($total, 2) . $plus_shipping . '</td> 
		</tr>   
     ';		
		// Complete the form, if appropriate:
		if ($action) 
        {
			echo' 
                <tr>
                <th colspan="6" align="center">                      
                <button type="submit" name="submit" value="update">Update Cart</button></th>
                </form>
                <form action="./orderinformation.php" method="post">
                <input type="hidden" name="authorized" value="authorized"/>
                <th><input type="submit" id="submit" name="submit" value="Checkout"><th>
                </form>  
                </tr>
                ';          
		}  
        echo '</form>';   
		echo '</table>';
		
	} // End of display_cart() method.
	
} // End of WidgetShoppingCart class.

?>
